#!/bin/bash

set -e

# 日志文件
PROGRESS_FILE="$HOME/install_zsh_tmux_progress.log"
touch "$PROGRESS_FILE"

# 颜色定义
RED='\e[91m'
GREEN='\e[92m'
YELLOW='\e[93m'
BLUE='\e[94m'
RESET='\e[0m'

# 判断是否需要使用 sudo
SUDO=''
if [ "$EUID" -ne 0 ]; then
    SUDO='sudo'
fi

# 检查步骤是否完成
function is_step_done() {
    grep -q "$1" "$PROGRESS_FILE"
}

# 标记步骤已完成
function mark_step_done() {
    echo "$1" >>"$PROGRESS_FILE"
}

# 标准的步骤输出
function step_start() {
    echo -e "${BLUE}==> $1${RESET}"
}

function step_done() {
    echo -e "${GREEN}==> $1 完成。${RESET}"
}

# 1. 更新系统
if ! is_step_done "update_system"; then
    step_start "更新系统"
    $SUDO apt update
    mark_step_done "update_system"
    step_done "系统更新"
fi

# 2. 安装 zsh
if ! is_step_done "install_zsh"; then
    step_start "安装 zsh"
    $SUDO apt install zsh -y
    mark_step_done "install_zsh"
    step_done "zsh 安装"
fi

# 3. 安装 Oh My Zsh
if ! is_step_done "install_oh_my_zsh"; then
    step_start "安装 Oh My Zsh"

    ZSH_DIR="$HOME/.oh-my-zsh"
    if [ -d "$ZSH_DIR" ] && git -C "$ZSH_DIR" rev-parse >/dev/null 2>&1; then
        echo -e "${YELLOW}Oh My Zsh 已存在，跳过安装。${RESET}"
    else
        [ -d "$ZSH_DIR" ] && rm -rf "$ZSH_DIR"
        $SUDO apt install curl git -y
        RUNZSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    fi

    mark_step_done "install_oh_my_zsh"
    step_done "Oh My Zsh 安装"
fi

# 4. 设置 ZSH_CUSTOM 变量
if ! is_step_done "set_zsh_custom"; then
    step_start "设置 ZSH_CUSTOM 变量"
    if grep -q "^# ZSH_CUSTOM=" $HOME/.zshrc; then
        sed -i 's|^# ZSH_CUSTOM=.*|ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"|' $HOME/.zshrc
    elif grep -q "^ZSH_CUSTOM=" $HOME/.zshrc; then
        sed -i 's|^ZSH_CUSTOM=.*|ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"|' $HOME/.zshrc
    else
        echo 'ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"' >>$HOME/.zshrc
    fi
    mark_step_done "set_zsh_custom"
    step_done "ZSH_CUSTOM 变量设置"
fi

# 5. 安装 Powerlevel10k 主题
if ! is_step_done "install_powerlevel10k"; then
    step_start "安装 Powerlevel10k 主题"

    ZSH_CUSTOM_DIR="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
    if [ -d "$ZSH_CUSTOM_DIR" ] && git -C "$ZSH_CUSTOM_DIR" rev-parse >/dev/null 2>&1; then
        echo -e "${YELLOW}Powerlevel10k 主题已存在，跳过安装。${RESET}"
    else
        [ -d "$ZSH_CUSTOM_DIR" ] && rm -rf "$ZSH_CUSTOM_DIR"
        git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$ZSH_CUSTOM_DIR"
        sed -i 's/^ZSH_THEME=.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' $HOME/.zshrc
    fi

    mark_step_done "install_powerlevel10k"
    step_done "Powerlevel10k 主题安装"
fi

# 6. 安装 MesloLGS NF Regular 字体
if ! is_step_done "install_nerd_fonts"; then
    FONT_NAME="MesloLGS NF"
    FONT_FILE="MesloLGS NF Regular.ttf"
    FONT_URL="https://gitee.com/Huanfly/share_lib/raw/main/MesloLGS%20NF%20Regular.ttf"

    step_start "检查并安装字体 $FONT_NAME"

    if fc-list | grep -qi "$FONT_NAME"; then
        echo -e "${YELLOW}$FONT_NAME 字体已存在，跳过安装。${RESET}"
    else
        mkdir -p "$HOME/.local/share/fonts"
        curl -fLo "$HOME/.local/share/fonts/$FONT_FILE" "$FONT_URL"
        fc-cache -fv
        echo -e "${GREEN}$FONT_NAME 字体已安装。${RESET}"
    fi

    mark_step_done "install_nerd_fonts"
    step_done "$FONT_NAME 字体安装"
fi

# 7. 安装 tmux
if ! is_step_done "install_tmux"; then
    step_start "安装 tmux"
    $SUDO apt install tmux -y
    mark_step_done "install_tmux"
    step_done "tmux 安装"
fi

# 8. 安装 Oh My Tmux
if ! is_step_done "install_oh_my_tmux"; then
    step_start "安装 Oh My Tmux"

    if [ -d "$HOME/.tmux" ] && git -C "$HOME/.tmux" rev-parse >/dev/null 2>&1; then
        echo -e "${YELLOW}Oh My Tmux 已存在，跳过安装。${RESET}"
    else
        [ -d "$HOME/.tmux" ] && rm -rf "$HOME/.tmux"
        git clone https://github.com/gpakosz/.tmux.git "$HOME/.tmux"
        ln -s -f "$HOME/.tmux/.tmux.conf" "$HOME/.tmux.conf"
        cp "$HOME/.tmux/.tmux.conf.local" "$HOME/.tmux.conf.local"
    fi

    mark_step_done "install_oh_my_tmux"
    step_done "Oh My Tmux 安装"
fi

# 9. 安装 zsh 插件：zsh-autosuggestions 和 zsh-syntax-highlighting
if ! is_step_done "install_zsh_plugins"; then
    step_start "安装 zsh 插件"

    plugins=(zsh-autosuggestions zsh-syntax-highlighting)
    for plugin in "${plugins[@]}"; do
        PLUGIN_DIR="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/$plugin"
        if [ -d "$PLUGIN_DIR" ] && git -C "$PLUGIN_DIR" rev-parse >/dev/null 2>&1; then
            echo -e "${YELLOW}$plugin 已存在，跳过安装。${RESET}"
        else
            [ -d "$PLUGIN_DIR" ] && rm -rf "$PLUGIN_DIR"
            git clone https://github.com/zsh-users/$plugin "$PLUGIN_DIR"
        fi
    done

    sed -i 's/^plugins=.*/plugins=(git zsh-autosuggestions zsh-syntax-highlighting)/' $HOME/.zshrc
    mark_step_done "install_zsh_plugins"
    step_done "zsh 插件安装"
fi

# 10. 替换本地的 .tmux.conf.local
if ! is_step_done "replace_tmux_conf_local"; then
    step_start "替换 .tmux.conf.local"

    if [ -f "$HOME/.tmux.conf.local" ]; then
        read -p "发现现有的 .tmux.conf.local，是否替换？(y/n): " choice
        if [[ "$choice" =~ ^[Yy]$ ]]; then
            mv "$HOME/.tmux.conf.local" "$HOME/.tmux.conf.local.bak_$(date +%Y%m%d_%H%M%S)"
            curl -fLo "$HOME/.tmux.conf.local" "https://gitee.com/Huanfly/share_lib/raw/main/.tmux.conf.local"
        else
            echo -e "${YELLOW}跳过替换 .tmux.conf.local 文件。${RESET}"
        fi
    else
        curl -fLo "$HOME/.tmux.conf.local" "https://gitee.com/Huanfly/share_lib/raw/main/.tmux.conf.local"
    fi

    mark_step_done "replace_tmux_conf_local"
    step_done ".tmux.conf.local 替换"
fi

# 删除日志文件
if [ -f "$PROGRESS_FILE" ]; then
    echo -e "${BLUE}清理日志文件...${RESET}"
    rm "$PROGRESS_FILE"
    echo -e "${GREEN}日志文件已删除。${RESET}"
fi

# 完成安装
echo -e "${GREEN}所有安装步骤已完成！请注销并重新登录以应用更改。${RESET}"
echo -e "${YELLOW}请将终端字体更改为 MesloLGS NF，以支持 Powerlevel10k 和 tmux。${RESET}"
echo -e "${GREEN}你可以运行 'p10k configure' 重新配置 Powerlevel10k 主题样式。${RESET}"
